﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KCMS.BusinessLogic.Interfaces;
using KCMS.BusinessLogic.Models;
using StructureMap;
using KCMS.Areas.Admin.Engine;
using KCMS.Areas.Admin.Models;
using System.Web.Helpers;


namespace KCMS.Areas.Admin.Controllers
{
    public class ProductImageController : Controller
    {
        private IProductImageService pService = ObjectFactory.GetInstance<IProductImageService>();
        private IProductService productService = ObjectFactory.GetInstance<IProductService>();
        private ProductListGenerator productListGenerator = new ProductListGenerator();
        public ActionResult Index()
        {
            ViewData["products"] = productListGenerator.getGenderListWithEmptyOption(productService.getAll());
            var model = pService.getAll();
            return View(model);
        }
        public ActionResult GetByProduct(Guid productId)
        {
            List<ProductImage> model = new List<ProductImage>();
            if (productId.Equals(Guid.Empty))
            {
                model.AddRange((from t in pService.getAll()
                                select t).ToList());
            }
            else
            {
                model.AddRange((from t in pService.getAll()
                                where t.ProductId.Equals(productId)
                                select t).ToList());
            }

            ViewData["products"] = productListGenerator.getGenderListWithEmptyOption(productService.getAll());
            return PartialView("_ProductImageList", model);
        }
        [HttpGet]
        public ActionResult Create()
        {
            var model = new ProductImageEditorModel();
            model.ProductList = productListGenerator.getGenderList(productService.getAll(), Guid.Empty);
            return View(model);
        }
        [HttpGet]
        public ActionResult CreateByProduct(Guid productId)
        {

            var model = new ProductImageEditorModel();
            model.ProductList = productListGenerator.getGenderList(productService.getAll(), Guid.Empty);
            model.ProductImage.ProductId = productId;
            model.ProductImage.Product = productService.getById(productId);
            return View("Create", model);
        }
        [HttpPost]
        public ActionResult Create(ProductImageEditorModel model,FormCollection collection)
        {
            
            WebImage photo = WebImage.GetImageFromRequest("image");
            WebImage photoThumb = photo.Clone().Resize(width: 156, height:156, preserveAspectRatio: true, preventEnlarge: true);
            photoThumb.Crop(1, 1, 0, 0);
            ProductImage productImage = model.ProductImage;
            ImageUploader uploader=new ImageUploader(new ProductImagePathGenerator(productImage));            
            productImage.ImageFile = uploader.Save(photo);
            productImage.ColorImageFile = uploader.Save(photoThumb,true);
            pService.save(productImage);
            if (collection["onSaveAndCreate"] != null)
                return RedirectToAction("CreateByProduct", new { productId = productImage.ProductId });
            else
                return RedirectToAction("Index","Product");
        }
        [HttpGet]
        public ActionResult Edit(Guid id)
        {
            var productImage = pService.getById(id);
            var model = new ProductImageEditorModel(productImage);
            model.ProductList = model.ProductList = productListGenerator.getGenderList(productService.getAll(), Guid.Empty);
            return View(model);
        }
        [HttpPost]
        public ActionResult Edit(ProductImageEditorModel model)
        {
            WebImage photo = WebImage.GetImageFromRequest("image");
            WebImage photoThumb = photo.Clone().Resize(width: 156, height: 156, preserveAspectRatio: true, preventEnlarge: true);
            photoThumb.Crop(1, 1, 0, 0);
            ProductImage productImage = model.ProductImage;
            ImageUploader uploader = new ImageUploader(new ProductImagePathGenerator(productImage));
            ProductImage last = pService.getById(productImage.Id);
            if (photo != null)
            {
                productImage.ImageFile = uploader.Update(photo, last.ImageFile);
                productImage.ColorImageFile = uploader.Update(photoThumb, last.ColorImageFile, true);
            }
            //if (colorImage != null)
            //{
            //    productImage.ColorImageFile = uploader.UpdateImage(colorImage, last.ColorImageFile, productImage, "thumb");
            //}
            pService.update(productImage);
            return RedirectToAction("Index");
        }
        [HttpGet]
        public ActionResult Delete(Guid id)
        {
            var model = pService.getById(id);
            return View(model);
        }
        [HttpPost]
        public ActionResult Delete(ProductImage model)
        {
            ImageUploader uploader = new ImageUploader(new ProductImagePathGenerator(model));
            uploader.Delete(model.ImageFile);
            uploader.Delete(model.ColorImageFile);
            pService.delete(model.Id);
            return RedirectToAction("Index");
        }
    }
}
